import request from '@/utils/request'

// 查询【请填写功能名称】列表
export function listReport(query) {
  return request({
    url: '/equipment/report/list',
    method: 'get',
    params: query
  })
}

// 查询【请填写功能名称】详细
export function getReport(id) {
  return request({
    url: '/equipment/report/' + id,
    method: 'get'
  })
}

export function getFileName(id) {
  return request({
    url: '/equipment/report/getFileName?id=' + id,
    method: 'get'
  })
}

export function doDownloadReportFile(id) {
  getFileName(id).then(resp => {
    const fileName = resp.data;


    request({
      url: '/equipment/report/downloadFile?id=' + id,
      method: 'get',
      responseType: 'blob'
    }).then(response => {
      let blob = new Blob([response]) // 转成文本存储
      let a = document.createElement('a') // 转换完成，创建一个a标签用于下载

      if (fileName !== null) {
        a.download = fileName
      } else {
        let s = response.getHeaders()['content-disposition']
        a.download = decodeURI(s.split('filename=')[1])
      }

      a.href = window.URL.createObjectURL(blob) // 转成本地连接到blob文本
      a.click()
    })

    // fetch(,{
    //
    // }).then(resp=>{
    //   resp.json().then((json)=>{
    //     console.log('resp::',json)
    //   })
    //
    //   return resp.blob()
    // }).then(blob=>{
    //   console.log('done ',blob)
    //   let a = document.createElement('a');
    //   let url = window.URL.createObjectURL(blob);
    //
    //   a.href = url;
    //   a.download = filename;
    //   a.click();
    //   window.URL.revokeObjectURL(url);
    // })
  })


}

// 新增【请填写功能名称】
export function addReport(data) {
  let formData = new FormData();

  for (let key in data) {
    formData.append(key, data[key])
  }

  return request({
    url: '/equipment/report',
    method: 'post',
    data: formData,

  })
}

// 修改【请填写功能名称】
export function updateReport(data) {
  return request({
    url: '/equipment/report',
    method: 'put',
    data: data
  })
}

// 删除【请填写功能名称】
export function delReport(id) {
  return request({
    url: '/equipment/report/' + id,
    method: 'delete'
  })
}
